#include "stdafx.h"
#include "Player.h"
#include "../../../EdgeControls\Input.h"
#include "../../../EdgeControls\Controls.h"
#include "../../../EdgeGraphics\Camera.h"
#include "../../Delegate.h"
#include <WinBase.h>
#include "../../../EdgeAudio/AudioManager.h"
#include "../../../EdgeAudio/SoundEffect2D.h"
#include "../../../EdgeRemote/IPAddress.h"
#include "../../../EdgeRemote/NetworkManager.h"
#include "../ControlableGameObject.h"
#include "../Human.h"

// static delta time instance for use in event functions
float delta_time = 0.f;

void move_forward(void**);
void move_backward(void**);
void move_left(void**);
void move_right(void**);

void attack(void**);
void block(void**);

void jump(void**);

void Player::InitControls(void)
{
	Input* pInput = Input::GetInstance();

	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().move_forward)->subscribers.push_back(Functor<key_event_handler>(&move_forward, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().move_backward)->subscribers.push_back(Functor<key_event_handler>(&move_backward, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().move_left)->subscribers.push_back(Functor<key_event_handler>(&move_left, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().move_right)->subscribers.push_back(Functor<key_event_handler>(&move_right, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().jump)->subscribers.push_back(Functor<key_event_handler>(&jump, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().primary_fire)->subscribers.push_back(Functor<key_event_handler>(&attack, 0));
	pInput->getKeyBindings(Controls::GetInstance().GetInstance().GetInstance().block)->Subscribe(Functor<key_event_handler>(&block, 0));
}

void attack(void**)
{
#undef PlaySound
	Player::GetInstance()->setScale(3.f);
	//if (!Input::GetInstance()->wasKeyDown(MouseButtons::Left))
		//AudioManager::GetInstance()->PlaySound(Player::GetInstance()->sound);
}

void block(void**)
{
	Player::GetInstance()->setScale(0.1f);
}

void jump(void**)
{
	/***********************Temporary********************************/
	// check to ensure the player is on the ground
	if (Player::GetInstance()->getTarget()->getPosition().y > 5.f)
		return;
	/****************************************************************/
	Vector3 velocity = Player::GetInstance()->getTarget()->getVelocity();
	velocity += VectorHelper::vector_3::up * 5.f;
	Player::GetInstance()->getTarget()->setVelocity(velocity);
}

void Player::UpdateControls(float dt)
{
	delta_time = dt;
	// make the camera follow and look at the player
	Camera::GetInstance()->setTarget(human->getPosition());
	
	/***********************Temporary********************************/
	if (Input::GetInstance()->isKeyPressed(Keys::F5))
	{
		NetworkManager::GetInstance()->ConnectToSession(IPAddress(72, 238, 57, 4));
	}
	/****************************************************************/
}

void move_forward(void**)
{
	Vector3 position = Player::GetInstance()->getTarget()->getPosition();
	Vector3 move = Camera::GetInstance()->getForwardVector();
	// remove the y axis from controls
	move.y = 0.f;
	move.Normalize();
	move = move * delta_time * -Player::GetInstance()->getMoveSpeed();
	
	Vector3 delta = Player::GetInstance()->getTarget()->getVelocity() + move;
	Player::GetInstance()->getTarget()->setVelocity(delta);
}

void move_backward(void**)
{
	Vector3 position = Player::GetInstance()->getTarget()->getPosition();
	Vector3 move = Camera::GetInstance()->getForwardVector();
	// remove the y axis from controls
	move.y = 0.f;
	move = move * delta_time * Player::GetInstance()->getMoveSpeed();

	Vector3 delta = Player::GetInstance()->getTarget()->getVelocity() + move;
	Player::GetInstance()->getTarget()->setVelocity(delta);
}

void move_left(void**)
{
	Vector3 position = Player::GetInstance()->getTarget()->getPosition();
	Vector3 move = Camera::GetInstance()->getRightVector();
	// remove the y axis from controls
	move.y = 0.f;
	move = move * delta_time * Player::GetInstance()->getMoveSpeed();

	Vector3 delta = Player::GetInstance()->getTarget()->getVelocity() + move;
	Player::GetInstance()->getTarget()->setVelocity(delta);
}

void move_right(void**)
{
	Vector3 position = Player::GetInstance()->getTarget()->getPosition();
	Vector3 move = Camera::GetInstance()->getRightVector();
	// remove the y axis from controls
	move.y = 0.f;
	move = move * delta_time * -Player::GetInstance()->getMoveSpeed();

	Vector3 delta = Player::GetInstance()->getTarget()->getVelocity() + move;
	Player::GetInstance()->getTarget()->setVelocity(delta);
}